package com.ivy.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.ivy.dao.FriendDao;
import com.ivy.entity.Friend;
import com.ivy.utils.JdbcUtils;

public class FriendDaoImpl implements FriendDao{

	@Override
	public void insert(Friend friend) {
		String sql = "insert into friend(friend_tel,owner_tel) values(?,?);";
		QueryRunner qr = JdbcUtils.getQueryRunner();
		
		try {
			qr.update(sql,friend.getFriend_tel(),friend.getOwner_tel());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Friend> findFriendByTelephone(String owner_tel) {
		//select * from friend where owner_tel = '1233';
		String sql = "select * from friend where owner_tel = '" + owner_tel + "';";
		List<Friend> friends = null;
		try {
			QueryRunner qr = JdbcUtils.getQueryRunner();
			friends = qr.query(sql, new BeanListHandler<Friend>(Friend.class));
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return friends;
	}

	
}
